//
//  HHEnterPhoneNumberViewController.m
//  HHBroker
//
//  Created by He Wei on 9/30/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHEnterPhoneNumberViewController.h"
#import "NSString+ValidateArgument.h"
#import "HttpRequestManager.h"
#import "HHEnterCodeViewController.h"

@interface HHEnterPhoneNumberViewController () <UITextFieldDelegate,UIAlertViewDelegate>

@property (nonatomic, weak) IBOutlet UIButton *nextButton;
@property (nonatomic, weak) IBOutlet UITextField *phoneTextField;

@end

@implementation HHEnterPhoneNumberViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更换绑定手机";
    
    
}



#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //
    
        
    
    HHEnterCodeViewController *enterCode = [[HHEnterCodeViewController alloc] init];
    enterCode.readAuthorCodePhone = self.phoneTextField.text;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    [self.navigationController pushViewController:enterCode animated:YES];
    return;
    
    //
    NSString *newPh = _phoneTextField.text;
    NSString *urlString = [NSString stringWithFormat:@"%@&mobile=%@", KUrlChangePhoneGetCode, newPh];
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
            hub.mode = MBProgressHUDModeCustomView;
            hub.customView = imageView;
            hub.labelText = NSLocalizedString(@"发送成功，请查看短信", nil);
            hub.completionBlock = ^ {
                HHEnterCodeViewController *enterCode = [[HHEnterCodeViewController alloc] init];
                enterCode.readAuthorCodePhone = self.phoneTextField.text;
                [self.navigationController pushViewController:enterCode animated:YES];
                
                
            };
            [hub hide:YES afterDelay:2];
        } else {
            NSString *string = [response objectForKey:@"msg"];
            hub.labelText = string;
            [hub hide:YES afterDelay:2];
        }
    }
    failure:^(NSError *error) {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = @"网路有点问题";
            [hub hide:YES afterDelay:2];
    } isLogin:YES];
}

#pragma mark - Event

- (IBAction)textFieldValueChanged:(UITextField *)sender
{
    self.nextButton.enabled = sender.text.length > 0 ? YES : NO;
}


- (IBAction)nextButtonClicked:(UIButton *)sender
{
    NSString *newPh = self.phoneTextField.text;
    if (![NSString validateMobile:newPh ]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入合法的手机号码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
  
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确认手机号码" message:[NSString stringWithFormat:@"我们将发送验证码到这个号码:%@", newPh] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    [alertView show];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];


}


@end
